#include "ejmaplayer.h"
#include "ejstylesheet.h"
#include <QMenuBar>
#include <QToolBar>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QGraphicsPixmapItem>
#include <QTabWidget>
#include <QFile>

#include <QDebug>



EjMapLayer::EjMapLayer(QWidget *parent) : QWidget(parent)
{
    auto layout = new QVBoxLayout();

    this->initToolBar(layout);
    this->initWidget(layout);

    this->setLayout(layout);
    layout->setMargin(0);
    layout->setSpacing(0);
}

void EjMapLayer::initToolBar(QVBoxLayout * layout)
{
    auto toolBar = new QToolBar(this);
    toolBar->setMovable(false);
    layout->addWidget(toolBar);

    toolBar->addAction(QIcon("://res/png/page_new.png"), QObject::tr("新建"));
    toolBar->addAction(QIcon("://res/png/page_open.png"), QObject::tr("打开"));
    toolBar->addAction(QIcon("://res/png/page_save.png"), QObject::tr("保存"));
    toolBar->addAction(QIcon("://res/png/page_save_as.png"), QObject::tr("另存为"));
    toolBar->addSeparator();

    auto allLayer = toolBar->addAction(QIcon("://res/png/layer_all.png"), QObject::tr("所有图层"));
    auto surfaceLayer = toolBar->addAction(QIcon("://res/png/layer_surface.png"), QObject::tr("地表层"));
    auto objectLayer = toolBar->addAction(QIcon("://res/png/layer_object.png"), QObject::tr("物件层"));
    auto eventLayer = toolBar->addAction(QIcon("://res/png/layer_event.png"), QObject::tr("事件层"));
    toolBar->addSeparator();

    allLayer->setCheckable(true);
    surfaceLayer->setCheckable(true);
    objectLayer->setCheckable(true);
    eventLayer->setCheckable(true);

    allLayer->setChecked(true);
    surfaceLayer->setChecked(true);

    QActionGroup *actionGroup = new QActionGroup(this);
    actionGroup->addAction(surfaceLayer);
    actionGroup->addAction(objectLayer);
    actionGroup->addAction(eventLayer);

    connect(surfaceLayer, &QAction::triggered, [this](bool checked){if (checked) this->m_editType = SURFACE;});
    connect(objectLayer, &QAction::triggered, [this](bool checked){if (checked) this->m_editType = OBJECT;});
    connect(eventLayer, &QAction::triggered, [this](bool checked){if (checked) this->m_editType = EVENT;});

}

void EjMapLayer::initWidget(QVBoxLayout * layout)
{
    auto tab = new QTabWidget(this);
    layout->addWidget(tab);
    auto scene = new QGraphicsScene(tab);
    auto view = new QGraphicsView(scene, tab);
    tab->addTab(view, QObject::tr("地图一"));
    tab->setTabPosition(QTabWidget::South);
    view->setStyleSheet("background: transparent;border:0px; background-color: #000000;");

    auto item = scene->addPixmap(QPixmap("://res/button/main/focus.png"));
    item->setPos(0, 0);

    QFile baseFile("./theme/BaseTabWidget.qss");
    if (baseFile.open(QIODevice::ReadOnly | QIODevice::Text))
    {
        g_tabBarStyle = baseFile.readAll();
    }
    tab->setStyleSheet(g_tabBarStyle);
}


